package net.qunzhi.modules.historyspider;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
import net.qunzhi.modules.cms.entity.TbFlowStateEntity;
import net.qunzhi.modules.cms.entity.TbPressureStateEntity;
import net.qunzhi.modules.cms.service.SpiderService;
import net.qunzhi.modules.cms.service.TbFlowStateService;
import net.qunzhi.modules.cms.service.TbPressureStateService;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;

/*
*  created by cq 2019/10/24
*/
@Service
public class FlowSpiderCrawler extends BreadthCrawler {
    @Autowired private SpiderService spiderService  = new SpiderService();

    private static String crawlPath = "d://waterNetwork/spider/flowCraws";
    private Integer equipmentId;
    private String maxTime;

    public FlowSpiderCrawler() {
        // 设置断点路径
        super(crawlPath, false);

        //设置线程数,根据需求来搞
        setThreads(4);
        //  设置断点重启
        setResumable(false);
        this.getConf().setConnectTimeout(30000);
        this.getConf().setReadTimeout(30000);
        //添加正则表达式规则
        regexRule.addRule("http://.*");
    }

    public void setIdAndMaxtime(Integer equipmentId, String maxTime) {
        this.equipmentId = equipmentId;
        this.maxTime = maxTime;
    }

    @Override
    public void visit(Page page, CrawlDatums crawlDatums) {
        final Elements table = page.select("table");
        if (!"".equals(table.toString())) {
            Elements trs = table.get(4).select("tr");

            ArrayList<TbFlowStateEntity> list = new ArrayList<>();
            for (int i = 0; i < trs.size(); i++) {
                Elements tds = trs.get(i).select("td");
                ArrayList<String> arr = new ArrayList<>();

                for (int j = 0; j < tds.size(); j++) {
                    String text = tds.get(j).text();
                    arr.add(text);
                }

                if (!"".equals(arr.get(0)) && !"".equals(arr.get(1)) && !"".equals(arr.get(2))) {
                    //如果数据库最大时间比爬取到的时间小才进行插入操作
                    if (maxTime.compareTo(arr.get(0)) < 0) {
                        TbFlowStateEntity flowStateEntity = new TbFlowStateEntity();
                        flowStateEntity.setTime(LocalDateTime.parse(arr.get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
                                .setValue(arr.get(1))
                                .setState(arr.get(2))
                                .setEquipmentId(equipmentId);
                        list.add(flowStateEntity);
                    }
                }
            }
            if (list.size() > 0) {
                spiderService.insertFlowState(list);
            }
        }
    }
}